package com.ithema.reggie.common;

import com.ithema.reggie.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@ResponseBody
@ControllerAdvice(annotations = {Controller.class, RestController.class})
public class GlobalExceptionHandler {

    @ExceptionHandler
    public R<String> hanlderException(Exception ex){
        log.info("========异常处理======");
        ex.printStackTrace();
        return R.error("不好意思,我们正在升级~~");
    }
    //只处理 BusinessException 异常
    @ExceptionHandler(BusinessException.class)
    public R handleBusinessException(Exception ex){
        //打印异常信息
        ex.printStackTrace();

        //记录日志
        log.error(ex.getMessage());

        //生成响应
        return R.error(ex.getMessage());
    }

}
